package com.artron.mediaartron.ui.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.artron.baselib.base.BaseActivity;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;


/**
 * Doing
 */

public abstract class AppBaseActivity extends BaseActivity {
    @BindView(R.id.General_toolbar)
    protected Toolbar mToolbar;
    @BindView(R.id.General_title)
    protected TextView mTvTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        MobclickAgent.setDebugMode(true);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        final ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        decorView.post(new Runnable() {
                @Override
                public void run() {
                    View view = initLinkPageView(decorView);
                    if (view != null) {
                        decorView.addView(view);
                    }
                }
        });
    }

    protected View initLinkPageView(ViewGroup parent) {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initActionBar() {
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
//            AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
//            params.topMargin = UIUtils.getStatusBarHeight();
//            mToolbar.setLayoutParams(params);
        }

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
        }
        //TODO 这里的适配还没有做，只是适配了6.0以上的手机，以后来补充
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
//                && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //获取windowphone下的decorView
//            ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
//            int count = decorView.getChildCount();
//            //判断是否已经添加了statusBarView
//            if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {
//                decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
//            } else {
//                //新建一个和状态栏高宽的view
//                StatusBarView statusView = createStatusBarView(activity, color, statusBarAlpha);
//                decorView.addView(statusView);
//            }
//            ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
//            //rootview不会为状态栏留出状态栏空间
//            ViewCompat.setFitsSystemWindows(rootView, true);
//            rootView.setClipToPadding(true);
//        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (actionBar != null) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                getWindow().setStatusBarColor(UIUtils.getColor(R.color.white));

                //fitsSystemWindow 为 false, 不预留系统栏位置.
                ViewGroup mContentView = ((ViewGroup) getWindow().findViewById(Window.ID_ANDROID_CONTENT));
                View mChildView = mContentView.getChildAt(0);
                if (mChildView != null) {
                    ViewCompat.setFitsSystemWindows(mChildView, true);
                    ViewCompat.requestApplyInsets(mChildView);
                }
            }
        }

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.base_menu, menu);
        return true;
    }

    protected void toBackActivity() {
        Intent parentActivityIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, parentActivityIntent)) {
            TaskStackBuilder.create(this).addNextIntentWithParentStack(parentActivityIntent).startActivities();
        } else {
            parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpTo(this, parentActivityIntent);
        }
    }

}
